package is.web.xdumper.client.table;

import is.bean.entity.xdumper.session.XdumperSession;
import is.bean.entity.xdumper.session.XdumperSessionBeanRemote;
import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Iterator;

/**
 *
 * @author aspr0
 */
public class XdumperSessionTable
{
    public static String generate(Collection<XdumperSessionBeanRemote> remotes) throws RemoteException
    {
        String tableCanvas =
                "<form method=\"post\">"
                + "<table>"
                + "<thead>%s</thead>"
                + "<tbody>%s</tbody>"
                + "</table>"
                + "</form>";
        String thead =
                "<tr>"
                + "<td>Id</td>"
                + "<td>Action</td>"
                + "<td>Start time</td>"
                + "<td>End time</td>"
                + "<td>File</td>"
                + "<td></td>"
                + "</tr>";
        String rows = "";


        Iterator<XdumperSessionBeanRemote> i = remotes.iterator();

        while (i.hasNext())
        {
            XdumperSession xs = i.next().getData();

            String row =
                    "<tr>"
                    + "<td>" + xs.getId() + "</td>"
                    + "<td>" + xs.getAction() + "</td>"
                    + "<td>" + xs.getTimeStart() + "</td>"
                    + "<td>" + xs.getTimeEnd() + "</td>"
                    + "<td>"
                    + "<a href=\"out/" + xs.getFilePath() + "\" target=\"_blank\">" + xs.getFilePath() + "</a>"
                    + "</td>"
                    + "<td>"
                    + "<a href=\"?id=" + xs.getId() + "&act=delete\" title=\"delete\">"
                    + "<image src=\"../img/edit-delete.png\" class=\"hMargin\"></a>"
                    + "</td>"
                    + "</tr>";

            rows += row;
        }

        return String.format(tableCanvas, thead, rows);
    }
}
